//
//  CollectViewController.m
//  HappinessTrip
//
//  Created by dllo on 15/10/19.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "CollectViewController.h"
#import "UIImageView+WebCache.h"
#import "DataBaseManager.h"
#import "CollectCollectionViewCell.h"
#import "PlayDetailsViewController.h"
#import "Model.h"
#import "Fit.h"

@interface CollectViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UISearchBarDelegate>
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)NSMutableArray *dataArray;
@property(nonatomic, retain)UISearchBar *searchBar;

@end

@implementation CollectViewController
-(void)dealloc
{
    [_collectionView release];
    [_dataArray release];
    [_searchBar release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的收藏";
    [self changeColor];
    
    //初始化搜索框
    _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30 * H)];
    _searchBar.placeholder = @"请输入要搜索的内容";
    _searchBar.delegate = self;
    _searchBar.layer.cornerRadius = 25;
    _searchBar.layer.masksToBounds = YES;
    _searchBar.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.view addSubview:_searchBar];
    [_searchBar release];
    
}

-(void)changeColor{
    self.collectionView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = [Model shareInstance].BackgroundColor;
    [self.collectionView reloadData];
}

-(void)loadView
{
    [super loadView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"changeColor" object:nil];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(165 * W, 200 * H);
    flowLayout.minimumLineSpacing = 10 * H;
    flowLayout.minimumInteritemSpacing = 10 * W;
    flowLayout.sectionInset = UIEdgeInsetsMake(10 * W, 10 * H, 10 * W, 10 * H);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 30 * H, self.view.frame.size.width, self.view.frame.size.height - _searchBar.frame.size.height - 64) collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor colorWithRed:1 green:0.95 blue:0.85 alpha:1];
    [self.view addSubview:_collectionView];
    [_collectionView release];
    [_collectionView registerClass:[CollectCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];

    
}

-(void)viewWillAppear:(BOOL)animated
{
    self.dataArray = [DataBaseManager selectPlayList];
    if (_dataArray.count == 0) {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温心提示"message:@"暂无收藏,快去添加哦~"preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    self.tabBarController.tabBar.hidden = YES;
    [_collectionView reloadData];
}
-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    [_searchBar resignFirstResponder];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:[[_dataArray objectAtIndex:indexPath.row]title_page]]];
    PlayList *playList = [_dataArray objectAtIndex:indexPath.row];
    NSDictionary *dic = playList.user;
    [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"avatar_l"]]];
    cell.titleLabel.text = [NSString stringWithFormat:@"%@", [[_dataArray objectAtIndex:indexPath.row]title]];
    cell.timeLabel.text = [NSString stringWithFormat:@"%@ · %@喜欢", [[_dataArray objectAtIndex:indexPath.row] address], [[_dataArray objectAtIndex:indexPath.row] like_count]];
    NSString *string = [[[_dataArray objectAtIndex:indexPath.row]tab_list] componentsJoinedByString:@" "];
    cell.typeLabel.text = [NSString stringWithFormat:@"%@", string];
    cell.moneyLabel.text = [NSString stringWithFormat:@"%@", [[_dataArray objectAtIndex:indexPath.row]price]];

    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PlayDetailsViewController *playVC = [[PlayDetailsViewController alloc]init];
    playVC.play = _dataArray[indexPath.row];
    [self.navigationController pushViewController:playVC animated:YES];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    self.dataArray = [DataBaseManager selectAllPlayByMovieName: searchText];
    [_collectionView reloadData];
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
